import React, { Component } from 'react';
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';
import routes from './router';

export default class App extends Component {
    render() {
        return (
            <div className="app">
                <BrowserRouter>
                    <Switch>
                        <Redirect exact from="/" to="/blog/list" />
                        {routes.map((item) => {
                            return (
                                <Route
                                    key={item.path}
                                    path={item.path}
                                    render={(props) => {
                                        if (
                                            item.noLogin ||
                                            window.sessionStorage.getItem(
                                                'token',
                                            )
                                        ) {
                                            return (
                                                <item.component {...props} />
                                            );
                                        }
                                        return <Redirect to="/login" />;
                                    }}
                                />
                            );
                        })}
                    </Switch>
                </BrowserRouter>
            </div>
        );
    }
}
